﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FluffyUnderware.Curvy;

namespace Yoozoo.Gameplay.City.AI
{
    public class CityAIPathManager : MonoBehaviour
    {
        [SerializeField]
        private GameObject PathData;
        [SerializeField]
        private GameObject[] AuraPathData;

        private Dictionary<string, CurvySpline> splineMap;
        private List<CurvySpline> CurvyMap;
        private List<List<CurvySpline>> AuraCurvySpline;
        private List<string> unLockSplineList;
        private int tempCount = 0;
        private int tempAuraCount = 0;

        private void Awake()
        {
            splineMap = new Dictionary<string, CurvySpline>();
            CurvyMap = new List<CurvySpline>();
            AuraCurvySpline = new List<List<CurvySpline>>();
            unLockSplineList = new List<string>();
            if (PathData != null)
			{
                GameObject pathDataObj = GameObject.Instantiate(PathData);
                for (int i = 0; i < pathDataObj.transform.childCount; i++)
                {
                    CurvySpline spline = pathDataObj.transform.GetChild(i).GetComponent<CurvySpline>();
                    if (spline)
                    {
                        CurvyMap.Add(spline);
                        splineMap[spline.name] = spline;
                    }

                }
                pathDataObj.transform.parent = transform;
            }
            if (AuraPathData != null)
            {
				for (int i = 0; i < AuraPathData.Length; i++)
				{
                    GameObject pathDataObj = GameObject.Instantiate(AuraPathData[i]);
                    List<CurvySpline> splineList = new List<CurvySpline>();
                    for (int j = 0; j < pathDataObj.transform.childCount; j++)
                    {
                        CurvySpline spline = pathDataObj.transform.GetChild(j).GetComponent<CurvySpline>();
                        if (spline)
                        {
                            splineList.Add(spline);
                        }
                    }
                    AuraCurvySpline.Add(splineList);
                    pathDataObj.transform.parent = transform;
                }
                
            }
        }

        public void InitUnlockSpline(string splineName)
		{
			if (!unLockSplineList.Contains(splineName))
			{
                unLockSplineList.Add(splineName);
            }
        }


        public CurvySpline GetRandomCurvy()
		{
			//暂时还是先按顺序取
			//         if(unLockSplineList.Count > 1)
			//{
			//             int random = Random.Range(0, unLockSplineList.Count);
			//             string name = unLockSplineList[random];
			//             if (splineMap.ContainsKey(name)) return splineMap[name];
			//         }
			if (unLockSplineList.Count > 1 && tempCount <= unLockSplineList.Count)
			{
                string name = unLockSplineList[tempCount];
                if (splineMap.ContainsKey(name))
				{
                    tempCount += 1;
                    return splineMap[name];
                }
                    
            }
            return null;
        }

        public CurvySpline GetAuraCurvy(int areaId)
        {
            //暂时还是先按顺序取
            //if (unLockSplineList.Count > 1)
            //{
            //	int random = Random.Range(0, unLockSplineList.Count);
            //	string name = unLockSplineList[random];
            //	if (splineMap.ContainsKey(name)) return splineMap[name];
            //}
            
            if (AuraCurvySpline.Count <= areaId) return null;
            
            var AuraCurvyList = AuraCurvySpline[areaId];
           
            if (AuraCurvyList.Count > 1 && tempAuraCount <= AuraCurvyList.Count)
            {
                int random = Random.Range(0, AuraCurvyList.Count);
                var spline = AuraCurvyList[random];
				if (spline)
				{
                    AuraCurvySpline[areaId].Remove(spline);
                    return spline;
                }
            }
            return null;
        }

        public bool CheckSplineIsOpen(string splineName)
		{
			if (unLockSplineList.Contains(splineName))
			{
                return true;
			}
            return false;
		}

        public void InitTempCount()
		{
            tempCount = 0;

        }
    }
}

